﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SheXiangTou.Services
{
    public class RabbitMqService
    {
        public static void Publish_Local_Message_Async(string message, string exchange, string queue)
        {
            var factory1 = new ConnectionFactory
            {
                HostName = ConfigerService.MQLocalHostName, //主机名，Rabbit会拿这个IP生成一个endpoint，这个很熟悉吧，就是socket绑定的那个终结点。
                UserName = ConfigerService.MQLocalUserName, //默认用户名,用户可以在服务端自定义创建，有相关命令行
                Password = ConfigerService.MQLocalPassword //默认密码
            };
            Task.Run(() => {
                try
                {
                    using (var connection = factory1.CreateConnection())//发给本地MQ端
                    {
                        using (var channel = connection.CreateModel())
                        {
                            byte[] messageBodyBytes = Encoding.UTF8.GetBytes(message);
                            channel.BasicPublish(exchange, queue, null, messageBodyBytes);
                            channel.Close();
                        }
                        connection.Close();
                    }
                }
                catch{}
            });
        }
        public static void Publish_Cloud_Message_Async(string message, string exchange, string queue)
        {
            try
            {
                var factory2 = new ConnectionFactory
                {
                    HostName = ConfigerService.MQCloudHostName, //主机名，Rabbit会拿这个IP生成一个endpoint，这个很熟悉吧，就是socket绑定的那个终结点。
                    UserName = ConfigerService.MQCloudUserName, //默认用户名,用户可以在服务端自定义创建，有相关命令行
                    Password = ConfigerService.MQCloudPassword //默认密码
                };
                using (var connection = factory2.CreateConnection())//发给本地
                {
                    using (var channel = connection.CreateModel())
                    {
                        byte[] messageBodyBytes = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange, queue, null, messageBodyBytes);
                        channel.Close();
                    }
                    connection.Close();
                }
            }
            catch { }
        }
        /// <summary>
        /// 监听本地MQ信息
        /// </summary>
        /// <param name="exchange"></param>
        /// <param name="queue"></param>
        private IConnection MonitorLocalMQ()
        {
            IConnection connection = null;
            try
            {
                //创建本地mq连接工厂
                ConnectionFactory factory = new ConnectionFactory
                {
                    HostName = ConfigerService.MQLocalHostName, //主机名
                    UserName = ConfigerService.MQLocalUserName, //用户名
                    Password = ConfigerService.MQLocalPassword //密码  
                };
                connection = factory.CreateConnection();
                var channelR = connection.CreateModel();
                var consumerR = new EventingBasicConsumer(channelR);
                channelR.BasicConsume(ConfigerService.user_sxt_queue, true, consumerR);
                DateTime dt = DateTime.Now;
                consumerR.Received += (model, ea1) =>
                {
                    try
                    {
                        if ((DateTime.Now - dt).TotalSeconds < 2)
                            return;
                        string x = Encoding.UTF8.GetString(ea1.Body.ToArray());
                        string[] a = x.Split(',');
                        if (a.Length < 2)
                            return;
                        byte[] m = new byte[] { 0x7e, 0xff, 0x06, 0x3a, 0x00, byte.Parse(a[0]), 0x00, 0xef };
                        dt = DateTime.Now;
                    }
                    catch { }
                };
            }
            catch {}
            return connection;
        }
    }
}
